package cn.mahjong.service;

import cn.hserver.plugin.web.handlers.Ws;
import cn.hutool.json.JSONUtil;
import cn.mahjong.bean.MahJongBean;
import cn.mahjong.bean.MahJongUserBean;
import cn.mahjong.bean.PlayMsg;
import cn.mahjong.utils.IocUtil;
import cn.mahjong.utils.MahJongUtil;

import java.util.*;

/**
 * 麻将房间
 */
public class MahJongService {
    private final Map<String, MahJongUserBean> userBeans = new LinkedHashMap<>();
    private final Stack<MahJongBean> mahJongBeans = new Stack<>();

    public void close() {
        mahJongBeans.clear();
        for (MahJongUserBean value : userBeans.values()) {
            value.getWs().getCtx().close();
        }
        userBeans.clear();
    }


    public MahJongService() {
        List<MahJongBean> randMahJong = MahJongUtil.getRandMahJong();
        for (MahJongBean mahJongBean : randMahJong) {
            mahJongBeans.push(mahJongBean);
        }
    }


    /**
     * 游戏主持人，控制游戏进度和出牌顺序，胡牌，碰，杠，记分等工作
     */
    public void handlerEvent() {
        List<PlayService> childBean = IocUtil.getChildBean(PlayService.class);
        for (PlayService play : childBean) {
            play.handler(MahJongService.this);
        }
    }

    /**
     * 获取麻将张数
     *
     * @param size
     * @return
     */
    public List<MahJongBean> getMahJong(Integer size) {
        if (mahJongBeans.size() < size) {
            return null;
        }
        List<MahJongBean> data = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            data.add(mahJongBeans.pop());
        }
        data.sort(Comparator.comparing(MahJongBean::getType)
                .thenComparing(MahJongBean::getValue));
        return data;
    }

    /**
     * 获取麻将剩余数量
     *
     * @return
     */
    public Integer getMahJongSize() {
        return mahJongBeans.size();
    }

    /**
     * 获取房间用户数量
     *
     * @return
     */
    public Integer getUserSize() {
        return userBeans.size();
    }

    /**
     * 添加用户
     *
     * @param ws
     * @param username
     * @return
     */
    public void addUser(Ws ws, String username) {
        MahJongUserBean user = getUser(username);
        if (user == null && userBeans.size() < 4) {
            List<MahJongBean> mahJong = getMahJong(getUserSize() == 0 ? 14 : 13);
            MahJongUserBean mahJongUserBean = new MahJongUserBean();
            mahJongUserBean.setUsername(username);
            mahJongUserBean.setWs(ws);
            mahJongUserBean.setHasOpenCard(mahJong.size() == 14);
            mahJongUserBean.getCurrentMahJong().addAll(mahJong);
            userBeans.put(username, mahJongUserBean);
        } else {
            //挤掉下线
            user.getWs().getCtx().close();
            user.setWs(ws);
        }
        //给房间里人同步发牌
        List<MahJongUserBean> users = getUsers();
        for (MahJongUserBean mahJongUserBean1 : users) {
            mahJongUserBean1.getWs().send(JSONUtil.toJsonStr(new PlayMsg(mahJongUserBean1.getUsername(), PlayMsg.Type.DEAL, users)));
        }
    }

    /**
     * 获取用户列表
     *
     * @return
     */
    public List<MahJongUserBean> getUsers() {
        return userBeans.values().stream().toList();
    }

    /**
     * 获取具体用户
     *
     * @param username
     * @return
     */
    public MahJongUserBean getUser(String username) {
        return userBeans.get(username);
    }
}
